#include "battryinfo.h"
#include "ui_battryinfo.h"
#include <QDataStream>

BattryInfo::BattryInfo(int curIndex, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BattryInfo)
{
    ui->setupUi(this);

    curBat = curIndex;

    for(int i=0; i < 16; i++)
    {
        comBo[i] = NULL;
    }

    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->setRowCount(1);
    ui->tableWidget->setAlternatingRowColors(true);

    ui->tableWidget->verticalHeader()->setVisible(false);
    ui->tableWidget->horizontalHeader()->setVisible(false);

    for(int i=0;i<4;i++)
    {
        ui->tableWidget->setColumnWidth(i,195);
    }

    for(int i=0;i<1;i++)
    {
        ui->tableWidget->setRowHeight(i,40);
    }

    ui->tableWidget->setFont(QFont("Helvetica",16,1)); //设置字体

    ui->tableWidget->setItem(0,0,new QTableWidgetItem(QString("编号")));
    ui->tableWidget->item(0,0)->setTextAlignment(Qt::AlignCenter);
    ui->tableWidget->setItem(0,1,new QTableWidgetItem(QString("电压")));
    ui->tableWidget->item(0,1)->setTextAlignment(Qt::AlignCenter);
    ui->tableWidget->setItem(0,2,new QTableWidgetItem(QString("温度")));
    ui->tableWidget->item(0,2)->setTextAlignment(Qt::AlignCenter);
    ui->tableWidget->setItem(0,3,new QTableWidgetItem(QString("类型")));
    ui->tableWidget->item(0,3)->setTextAlignment(Qt::AlignCenter);

    batNum = 0;
//    for(int i=0; i < 16; i++)
//    {
//        comBo[i] = new QComboBox();
//        comBo[i]->addItem("空电池");
//        comBo[i]->addItem("正常电池");
//        comBo[i]->addItem("跨接线");
//        comBo[i]->addItem("带跨接线电池");

//        ui->tableWidget->setCellWidget(i+1,3, comBo[i]);
//        comBo[i]->setCurrentIndex(1);
//    }

//    for(int i=1; i < 17; i++)
//    {
//        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString("编号%1").arg(i)));
//        ui->tableWidget->item(i,0)->setTextAlignment(Qt::AlignCenter);
//    }
}

BattryInfo::~BattryInfo()
{
    delete ui;
}

void BattryInfo::rec_BatInfo(QString batID, QString dat)
{
    if(batID.indexOf("1235") != -1)
    {
        int bID = batID.right(2).toInt(&ok, 16);

        if(bID+4 > ui->tableWidget->rowCount()-1)
        {
            batNum = bID + 4;

            ui->tableWidget->setRowCount(batNum+1);

            for(int i=0; i < batNum+1; i++)
            {
                ui->tableWidget->setRowHeight(i,40);
            }

            for(int i=0; i < batNum; i++)
            {
                if(comBo[i] == NULL)
                {
                    comBo[i] = new QComboBox();
                    comBo[i]->addItem("空电池");
                    comBo[i]->addItem("正常电池");
                    comBo[i]->addItem("跨接线");
                    comBo[i]->addItem("带跨接线电池");

                    ui->tableWidget->setCellWidget(i+1,3, comBo[i]);
                    comBo[i]->setCurrentIndex(1);
                }
            }

            for(int i=1; i < batNum+1; i++)
            {
                ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString("编号%1").arg(i)));
                ui->tableWidget->item(i,0)->setTextAlignment(Qt::AlignCenter);
            }
        }


        QByteArray ba;
        ba.clear();
        QDataStream packet(&ba, QIODevice::ReadWrite);
        packet.setByteOrder(QDataStream::BigEndian);
        for(int i=0; i < dat.length(); i=i+2)
        {
            quint8 temp = dat.mid(i, 2).toInt(&ok, 16);
            packet << temp;
        }

        quint16 vo[4];
        packet.device()->seek(0);
        packet >> vo[0] >> vo[1] >> vo[2] >> vo[3];

        for(int i=(bID+1); i < (bID+1+4); i++)
        {
            //int vo = dat.mid((i-bID-1)*4, 2).toInt(&ok, 16)*256 + dat.mid((i-bID-1)*4+2, 2).toInt(&ok, 16);
            ui->tableWidget->setItem(i,1,new QTableWidgetItem(QString("%1 mv").arg(vo[i-bID-1])));
            ui->tableWidget->item(i,1)->setTextAlignment(Qt::AlignCenter);
        }
    }
    if(batID.indexOf("1236") != -1)
    {
        int bID = batID.right(2).toInt(&ok, 16);

        if(bID+4 > ui->tableWidget->rowCount()-1)
        {
            batNum = bID + 4;

            ui->tableWidget->setRowCount(batNum+1);

            for(int i=0; i < batNum+1; i++)
            {
                ui->tableWidget->setRowHeight(i,40);
            }

            for(int i=0; i < batNum; i++)
            {
                if(comBo[i] == NULL)
                {
                    comBo[i] = new QComboBox();
                    comBo[i]->addItem("空电池");
                    comBo[i]->addItem("正常电池");
                    comBo[i]->addItem("跨接线");
                    comBo[i]->addItem("带跨接线电池");

                    ui->tableWidget->setCellWidget(i+1,3, comBo[i]);
                    comBo[i]->setCurrentIndex(1);
                }
            }

            for(int i=1; i < batNum+1; i++)
            {
                ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString("编号%1").arg(i)));
                ui->tableWidget->item(i,0)->setTextAlignment(Qt::AlignCenter);
            }
        }

        QByteArray ba;
        ba.clear();
        QDataStream packet(&ba, QIODevice::ReadWrite);
        packet.setByteOrder(QDataStream::BigEndian);
        for(int i=0; i < dat.length(); i=i+2)
        {
            quint8 temp = dat.mid(i, 2).toInt(&ok, 16);
            packet << temp;
        }

        quint16 vo[4];
        packet.device()->seek(0);
        packet >> vo[0] >> vo[1] >> vo[2] >> vo[3];

        for(int i=(bID+1); i < (bID+1+4); i++)
        {
            //int vo = dat.mid((i-bID-1)*4, 2).toInt(&ok, 16)*256 + dat.mid((i-bID-1)*4+2, 2).toInt(&ok, 16);
            ui->tableWidget->setItem(i,2,new QTableWidgetItem(QString("%1 mv").arg(vo[i-bID-1])));
            ui->tableWidget->item(i,2)->setTextAlignment(Qt::AlignCenter);
        }
    }
}
